package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class UserController {
    @Autowired
    UserDao userDao;
    Map<String, Object> map = new HashMap<>();
    @RequestMapping("/WorldCupApp/user/regist")
    @ResponseBody
    public Map<String,Object> register(@RequestBody User user){
        User savedUser = userDao.save(user);
        if (savedUser!=null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;


    }
    @RequestMapping("/WorldCupApp/user/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody User user){
        User userFound = userDao.findOne(Example.of(user)).orElse(null);
        Map<String,Object> map=null;
        if (userFound!=null){
            map = new HashMap<>();
            map.put("success",true);
            map.put("msg","成功");

        }else {
            map = new HashMap<>();
            map.put("success", false);
            map.put("msg","失败");
        }
        return map;

    }
}
